package com.jogern.baselibrary;

import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.AnimRes;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;

import java.util.List;

import static android.view.ViewTreeObserver.*;

/**
 * Created by jogern on 2016/8/15 0015.
 */

public abstract class BaseActivity extends AppCompatActivity {
    private int[] customAnimations;

    private OnSoftKeyboardStateChangedListener mKeyboardStateChangedListener;

    private boolean mIsSoftKeyboardShowing;

    protected void setKeyboardStateChangedListener(
            OnSoftKeyboardStateChangedListener
                    keyboardStateChangedListener) {
        mKeyboardStateChangedListener = keyboardStateChangedListener;
    }

    private OnGlobalLayoutListener mLayoutChangeListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
//        //设置全屏
//        getWindow().setFlags(
//                WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                WindowManager.LayoutParams.FLAG_FULLSCREEN
//        );
//        //设置横屏
//        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
        Manager.addCurrentActivity(this);
        super.onCreate(savedInstanceState);
        mLayoutChangeListener = new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (mKeyboardStateChangedListener != null) {
                    int screenHeight = getResources().getDisplayMetrics().heightPixels;
                    //判断窗口可见区域大小
                    Rect r = new Rect();
                    getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
                    //如果屏幕高度和Window可见区域高度差值大于整个屏幕高度的1/3，则表示软键盘显示中，否则软键盘为隐藏状态。
                    int heightDifference = screenHeight - (r.bottom - r.top);
                    boolean isKeyboardShowing = heightDifference > screenHeight / 3;

                    //如果之前软键盘状态为显示，现在为关闭，或者之前为关闭，现在为显示，则表示软键盘的状态发生了改变
                    if ((mIsSoftKeyboardShowing && !isKeyboardShowing) || (!mIsSoftKeyboardShowing && isKeyboardShowing)) {
                        mIsSoftKeyboardShowing = isKeyboardShowing;
                        mKeyboardStateChangedListener.OnSoftKeyboardStateChanged(
                                mIsSoftKeyboardShowing, heightDifference);
                    }
                }
            }
        };
        //注册布局变化监听
        getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(mLayoutChangeListener);
    }

    /** 增加Fragment */
    protected void addFragmentInStack(int contentId, @NonNull Fragment fragment) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        if (customAnimations != null) {
            ft.setCustomAnimations(customAnimations[0], customAnimations[1],
                    customAnimations[2], customAnimations[3]
            );
        }
        String tag = fragment.getClass().getName();
        ft.replace(contentId, fragment, tag).addToBackStack(tag).commitAllowingStateLoss();
    }

    protected void addFragmentInContainer(int contentId, @NonNull Fragment fragment) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        String tag = fragment.getClass().getName();
        ft.replace(contentId, fragment, tag).commitAllowingStateLoss();
    }

    /** 设置Fragment的跳转动画 */
    protected void setAddCustomAnimations(@AnimRes int enter, @AnimRes int exit,
                                          @AnimRes int popEnter, @AnimRes int popExit) {
        if (customAnimations == null) {
            customAnimations = new int[4];
        }
        customAnimations[0] = enter;
        customAnimations[1] = exit;
        customAnimations[2] = popEnter;
        customAnimations[3] = popExit;
    }

    /** 移除栈顶的Fragment */
    protected void removePopFragmentOfStack() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        int entryCount = fragmentManager.getBackStackEntryCount();
        if (entryCount > 1) {
            fragmentManager.popBackStack();
        }
    }


    /** 获取栈顶的Fragment */
    protected Fragment getPopFragmentOfStack() {
        FragmentManager manager = getSupportFragmentManager();
        List<Fragment> fragments = manager.getFragments();
        if (fragments != null && fragments.size() > 0) {
            return fragments.get(fragments.size() - 1);
        }
        return null;
    }


    @Override
    protected void onDestroy() {
        //移除布局变化监听
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            getWindow().getDecorView().getViewTreeObserver().removeOnGlobalLayoutListener(mLayoutChangeListener);
        } else {
            getWindow().getDecorView().getViewTreeObserver().removeGlobalOnLayoutListener(mLayoutChangeListener);
        }
        Manager.removeCurrentActivity(this);
        super.onDestroy();
    }
}
